<?php
//
// +--------------------------------------+
// |PHP version 5.5
// +--------------------------------------+
// |知果汇后台登录模块
// +--------------------------------------+
// |2017-12-26
// +--------------------------------------+
// |潘龙兵
// +--------------------------------------+
// |知果汇前台
// +--------------------------------------+
//
namespace app\api\controller;

use think\View;
use think\Controller;
use think\Db;
use app\data\Data;
use app\api\model\Common;

class User extends Index
{
    /*
    * @知果汇前台
    * @注册
    * @2018-01-17
    * @潘龙兵
    * @$add:注册数据
    *  $token:登录标识
    *  $data:返回数据
    */
    public function register()
    {
        if (!isset($_GET['message']) || !isset($_GET['password']) || !isset($_GET['status'])) {
            $this->_jsonp("1000", "参数错误", null);
        }
        $password = $_GET['password'];
        $_data = model('Common')->pwd($password);
        $data = json_decode($_data, true);
        // $data['password'] = $info['password'];
        $msg = $_GET['message'];
        $status = $_GET['status'];
        if ($status == 0) {
            $where['phone'] = $msg;
            $name = '手机号';
            $data['phone'] = $msg;
        } else {
            $where['email'] = $msg;
            $name = '邮箱';
            $data['email'] = $msg;
        }
        $zgh_user = new Data('user');
        $count = $zgh_user->count($where);
        if ($count != 0) $this->_jsonp("1000", "该" . $name . "已被注册", null);
        $data['username'] = 'zgh_' . $msg;
        $data['time'] = time();
        $data['role'] = 0;
        $res = $zgh_user->insertData($data);
        // dump($res);exit;
        if (!$res) $this->_jsonp("1000", "注册失败", null);
        $info['uid'] = $res;
        $info['username'] = $data['username'];
        $this->_jsonp("200", "注册成功", $info);
    }

    /*public function asd(){
      echo 123456;exit;
      $data = $this->pwd();
      dump(123);exit;
    }*/
    /*
    * @知果汇前台
    * @注册验证手机号&邮箱
    * @2018-01-26
    * @潘龙兵
    *  $message:邮箱&手机号
    *  $status:(0:手机号；1:邮箱)
    *  $data:返回数据
    */
    public function check_user()
    {
        if (!isset($_GET['message']) || !isset($_GET['status'])) {
            $this->_jsonp("1000", "参数错误");
        }

        $status = $_GET['status'];
        $msg = $_GET['message'];

        if ($status == 0) {
            $where['phone'] = $msg;
            $name = '手机号';
        } else {
            $where['email'] = $msg;
            $name = '邮箱';
        }
        // dump($where);exit;
        $zgh_user = new Data('user');
        $count = $zgh_user->count($where);
        // dump($count);exit;
        if ($count != 0) $this->_jsonp("1000", "该" . $name . "已被注册", null);
        $this->_jsonp("200", "改账号未被注册", null);
    }


    /*
   * @知果汇
   * @前台登录操作
   * @2017-01-24
   * @潘龙兵
   * @$user：post得到的用户名
   *  $password：post得到的密码
   *  $verify: 验证码
   *  $rand: 随机数
   *  $token:  登录标识,存储SESSIONID
   *  $id:  用户id
   *  $username:  用户名
   *  $zgh_user_info: 存储登录信息，json格式
   */
    public function login()
    {
        $user = input('get.username');
        $password = input('get.password');
        $where['username|email|phone'] = $user;
        $where['status'] = 1;
        $zgh_user = new Data('user');
        $field = 'uid,username,rand,password,static';
        $result = $zgh_user->_find($where, $field);
        // dump($result);exit;
        if (!$result) $this->_jsonp('1000', "用户名错误", null); //用户不存在
		 if ($result['static']==2) $this->_jsonp('1000', "账号已被禁用", null); //用户被禁用
        // if($result['role'] != 0) $this->json('1000','当前没有注册身份',null);
        $rand = $result['rand'];
        $password = model('Common')->change_pwd($password, $rand);
        if ($password != $result['password']) $this->_jsonp('1000', '密码错误', null); //密码错误
        // dump($result);exit;
        session_start();
        $id = $result['uid'];
        $username = $result['username'];
        $zgh_user_info = array(
            'id' => $id,
            'username' => $username
        );
        $zgh_user_info = json_encode($zgh_user_info);
        // dump($zgh_user_info);exit;
        session('zgh_user_info', $zgh_user_info);  //设置session
        $this->_jsonp('200', '登录成功', $result);
    }


}